// pages/perMsg/perMsg.js
//请求接口，获取用户头像显示
var http = require("../../utils/util")
// http://api_dev.wanxikeji.cn
Page({

  /**
   * 页面的初始数据
   */
  data: {
    c:'',
    money:24.56,
    coin:15,
    discounts:1,
    myheadURL:"",
    myName:"",
    shippingAdd:[],
    token:''
  },
 getUserInfo:function(){

  wx.getSetting({
    withSubscriptions: true,
  })
   console.log("正在获取基本信息授权");

 } ,
 
 test:function(){
   var that = this
  wx.login({
    success (res) {
      var a = res.code
      http.myRequest('/api/codeExchangeOpenid',{ code: a },that.myFn)
      // http.myRequest('/api/login',{name:'admin',pw:'admin'},this.myFn)
    }
  })
 },
 tokenFn:function(){//页面加载获取token
  var that = this
  wx.getStorage({
    key: 'token',
    success(res){
      that.setData({
        token:res.data
      })//拿到token之后再次传入回调,请求地址列表,作用域问题只能使用that
      that.addList()
    }
  })
},
 addList:function(){//接收地址列表。只负责接收到地址
  var that = this
  if( that.data.token !=  ''){
      http.myRequest("/api/userAddressList",{token:that.data.token},listf)
  }else{
    console.log("请先授权！"); 
  }
  function listf(_list){
    var list = _list.data
    // console.log(list);  
    that.setData({
     shippingAdd:list
    })
    wx.setStorageSync('add', that.data.shippingAdd)
  }   
},
 myFn:function(_data){
  var b = _data.data.openid
  http.myRequest('/api/register',{name:'admin',   pw:'admin', openid:b},this.logFn)
},
logFn:function(data){
  console.log(data);
  
},
  balance:function(){
    wx.navigateTo({
      url: '../balance/balance',
    })
  },
  indentAll:function(event){//全部订单 方法
    console.log(event.currentTarget.dataset.id);
    wx.navigateTo({
      url: '../indent/indent?id='+event.currentTarget.dataset.id,
    })
  },
  siteAll:function(){//全部地址 跳转
    wx.navigateTo({
      url: '../addsite/addsite',
    })
    
  },
  
  addSite:function(){//添加地址 跳转
    wx.navigateTo({
      url: '../addsite/addsite',
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.tokenFn();
    let myData = wx.getStorageSync('my')
    this.setData({
      myName:myData.name,
      myheadURL:myData.url
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})